# encoding: utf-8
from sys import exit
freq_strings = (u"о", u"е", u"а", u"и", u"н",
                u".", " ", u",",
                u"т", u"с", u"р", u"в", u"л", u"к", u"м", u"д",
                u"п", u"у", u"я", u"ы", u"ь", u"г", u"з", u"б", u"ч", u"й", u"х", u"ж", u"!", u"ш", u"ю", u"ц", u"щ",
                u"э", u"ф", u"ъ", u"ё")

#test_message = u"Привет. Доехали отлично. Уже в Гусино, скоро садимся есть и спать. Счастливо!"
test_message = u"Дома"


def code_message(message_text):
    if type(message_text) != type(unicode()):
        print "Warning!\tOnly UTF-8 message text encoding!"
        exit()

    encoding_int_list = []

    for letter in message_text:
        encoding_int_list.append(freq_strings.index(letter.lower()))

    #print encoding_int_list
    bytes_list = []

    for number in encoding_int_list:
        hex_value = bin(number)
        bytes_list.append(hex_value)

    #print bytes_list

    char_octet = 5

    result_bin_strings = []
    for binary_string in bytes_list:
        result_bin_strings.append(binary_string[2:].zfill(char_octet))

    return result_bin_strings


def make_summ(encoded_binary_list):
    binary_string = "".join(encoded_binary_list)
    summer_list = []

    for i in xrange(len(binary_string) - 1):
        new_summ_element = int(binary_string[i], 2) ^ int(binary_string[i + 1])
        summer_list.append(new_summ_element)

    print summer_list


def decode_binary(binary_list):
    bit_list = []

    for zero_bin_string in binary_list:
        bit_list.append(bin(0) if "1" not in zero_bin_string else "0b%s" % zero_bin_string.lstrip("0"))

    int_list = []

    for bit_string in bit_list:
        int_list.append(int(bit_string, 2))

    string_list = []

    for char in int_list:
        string_list.append(freq_strings[char])

    message = "".join(string_list)
    return message


transmit_list = code_message(test_message)
print transmit_list
make_summ(transmit_list)

#print 1 ^ 1
#text = decode_binary(transmit_list)
#print text